package com.golden.procedure.controller;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.golden.procedure.domain.ProductDeliveryDetailDO;
import com.golden.procedure.domain.ProductDeliveryRecordDO;
import com.golden.procedure.domain.QueryRequest;
import com.golden.procedure.domain.SingleRequest;
import com.golden.procedure.domain.WorkOrderPickDO;
import com.golden.procedure.query.WorkOrderQueryDO;
import com.golden.procedure.result.PageResponse;
import com.golden.procedure.result.ResultDO;
import com.golden.procedure.result.SingleResponse;
import com.golden.procedure.service.IProductDeliveryService;
import com.golden.procedure.util.BonConstant;
import com.golden.procedure.util.EncryptedUtil;
import com.golden.procedure.util.HttpStatus;
import com.golden.procedure.util.TaskDefKey;
import com.google.gson.Gson;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 产品交货管理
* <b>Description: 工单交货列表、交货详情及交货记录等</b><br> 
* @author Reading.Reagan</a>
* @version 1.0
* @Note
* <b>ProjectName:</b> procedure
* <br><b>PackageName:</b> com.golden.procedure.controller
* <br><b>ClassName:</b> ProductDeliveryController
* <br><b>Date:</b> 2018年10月22日 下午4:13:08
 */
@RestController
@RequestMapping(value = "/api/delivery") 
public class ProductDeliveryController extends BaseController {

	@Autowired
	IProductDeliveryService productDeliveryService;

	public static String encodeObject(SingleResponse<WorkOrderPickDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	public static String encodePage(PageResponse<ProductDeliveryRecordDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	@ApiOperation(value = "产品交货列表", notes = "产品交货列表, 查看工单表中，状态为设计制作、产品交货节点的工单，且在设计制作单记录表中有数据的工单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "status", value = "状态（0-包含完成； 1-不包含已完成）", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "nameOrTel", value = "客户姓名或电话", required = true, dataType = "Integer") })
	@RequestMapping(value = "/query.json", method = RequestMethod.POST)
	public String query(@RequestBody QueryRequest<WorkOrderQueryDO> request) {

		// {"data":
		// {"pageRows":10,"status":0,"nameOrTel":""},"version":1,"token":"W57m+iUG3/D+y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}
		WorkOrderQueryDO query = request.getData();
		if (query.getPageRows() > 0) {
			query.setPage(true);
		}
		query.setTasks(Arrays
				.asList(new String[] { TaskDefKey.DESIGN_PRODUCT.getKey(), TaskDefKey.PRODUCT_DELIVERY.getKey() }));
		// 0-包含完成； 1-不包含已完成,
		if (query.getStatus() == 1) {
			// 工单服务状态：进行中
			query.setStatus(BonConstant.WORKORDER_STATUS_GOING);
		} else {
			// 所有（进行中、已完成，不包含已取消）
			query.setStatus(null);
		}

		PageResponse<ProductDeliveryRecordDO> response = new PageResponse<ProductDeliveryRecordDO>();
		ResultDO resultDO = productDeliveryService.query(query);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(1);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
			response.setData((List<ProductDeliveryRecordDO>) resultDO.getModel(ResultDO.FIRST_MODEL_KEY));
			if (query.isPage()) {
				response.setTotal((int) resultDO.getModel(ResultDO.SECOND_MODEL_KEY));
			}
		} else {
			// 查询失败
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}

		return encodePage(response, PLANINTEXT);
	}

	@ApiOperation(value = "交货详情列表", notes = "交货详情列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "workOrderID", value = "工单ID", required = true, dataType = "Integer"),
		@ApiImplicitParam(name = "businessNo", value = "业务单号", required = true, dataType = "String") })
	@RequestMapping(value = "/queryDetail.json", method = RequestMethod.POST)
	public String queryDetail(@RequestBody QueryRequest<ProductDeliveryRecordDO> request) {

		// {"data":
		// {"workOrderID":3},"version":1,"token":"W57m+iUG3/D+y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}
		ProductDeliveryRecordDO recordDO = request.getData();

		PageResponse<ProductDeliveryRecordDO> response = new PageResponse<ProductDeliveryRecordDO>();
		ResultDO resultDO = productDeliveryService.queryDetail(recordDO);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
			response.setData((List<ProductDeliveryRecordDO>) resultDO.getModel(ResultDO.FIRST_MODEL_KEY));
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodePage(response, PLANINTEXT);
	}

	@ApiOperation(value = "添加交货记录", notes = "添加交货记录")
	@ApiImplicitParams({ @ApiImplicitParam(name = "workOrderID", value = "工单ID", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "orderProductIDs", value = "订单商品IDs", required = true, dataType = "Integer[]"),
			@ApiImplicitParam(name = "deliveryType", value = "交货方式：上门取件；送货上门;", required = true, dataType = "String"),
			@ApiImplicitParam(name = "deliveryPlace", value = "交货地点", required = true, dataType = "String"),
			@ApiImplicitParam(name = "deliveryDateStr", value = "交货日期", required = true, dataType = "String"),
			@ApiImplicitParam(name = "receiver", value = "取件人", required = true, dataType = "String"),
			@ApiImplicitParam(name = "employeeID", value = "销售人员ID", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "comment", value = "交货备注", required = false, dataType = "String") })
	@RequestMapping(value = "/record.json", method = RequestMethod.POST)
	public String record(@RequestBody SingleRequest<ProductDeliveryDetailDO> request) {

		// {"data":{"workOrderID":3,"orderProductIDs":[305,306],
		// "deliveryType":0, "deliveryPlace":"客户地址", "employeeID":9,
		// "deliveryDateStr":"1539680400000","receiver":"王五","comment":"交货备注111"},
		// "version":1,"token":"W57m+iUG3/D+y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}
		ProductDeliveryDetailDO domain = request.getData();

		SingleResponse<WorkOrderPickDO> response = new SingleResponse<WorkOrderPickDO>();
		ResultDO resultDO = productDeliveryService.add(domain);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeObject(response, PLANINTEXT);
	}

}
